/**
 * 2015-4-2
 */
package com.yechaoa.pictureselectordemo.db;

import com.j256.ormlite.dao.Dao;
import com.yechaoa.pictureselectordemo.SusceptorApplication;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;



/**
 * @author wcy
 */
public class DataManager {
    private Dao<SuceptorOperationHistory, String> mHistoryDao;

    public static DataManager getInstance() {
        return SingletonHolder.instance;
    }

    private static class SingletonHolder {
        public static final DataManager instance = new DataManager();
    }

    private DataManager() {
        DBHelper dbHelper = new DBHelper(SusceptorApplication.getInstance().getApplicationContext());
        try {
            mHistoryDao = dbHelper.getDao(SuceptorOperationHistory.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void updateHistory(SuceptorOperationHistory searchInfo) {
        try {
//            不可能重复的
            SuceptorOperationHistory history;
            if (mHistoryDao.idExists(searchInfo.getOp_datetime())) {
                history = mHistoryDao.queryForId(searchInfo.getOp_datetime());
            } else {
                history = new SuceptorOperationHistory();
            }
            history.setPanhao_id_1(searchInfo.getPanhao_id_1());
            history.setPanhao_id_2(searchInfo.getPanhao_id_2());
            history.setPanhao_id_3(searchInfo.getPanhao_id_3());
            history.setChamber(searchInfo.getChamber());
            history.setOp_param(searchInfo.getOp_param());
            history.setPicture_of_op(searchInfo.getPicture_of_op());
            history.setOp_datetime(searchInfo.getOp_datetime());
            mHistoryDao.createOrUpdate(history);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<SuceptorOperationHistory> getHistoryList() {
        List<SuceptorOperationHistory> historyList = new ArrayList<>();
        for (SuceptorOperationHistory history : mHistoryDao) {
            historyList.add(0, history);
        }
        return historyList;
    }

//    public List<SuceptorOperationHistory> getUnCheckList() {
//        List<SuceptorOperationHistory> unCheckList = new ArrayList<>();
//        for (SuceptorOperationHistory history : mHistoryDao) {
//            if (!history.getIs_check().equals("1")) {
//                unCheckList.add(0, history);
//            }
//        }
//        return unCheckList;
//    }

    public boolean idExists(String id) {
        try {
            return mHistoryDao.idExists(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public void deleteById(String id) {
        try {
            mHistoryDao.deleteById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

//    public String getRemark(String id) {
//        try {
//            if (idExists(id)) {
//                SuceptorOperationHistory history = mHistoryDao.queryForId(id);
//                return history.getRemark();
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//    public void updateRemark(String id, String remark) {
//        try {
//            SuceptorOperationHistory history = mHistoryDao.queryForId(id);
//            history.setRemark(remark);
//            mHistoryDao.update(history);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//    }
}
